import { isWeb } from "@/module-00-codebase/pkg-02-tool";
import { createMemoryHistory, createBrowserHistory, History } from "history";
let history: History;
export class HistoryFactoryImpl {
  public push(route: string) {
    const currentLength = this.getLength();
    const index = this.__getHisArry().findIndex(h => h.pathname === route);
    if (index >= 0) {
      const goIndex = index - (currentLength - 1);
      if (goIndex !== 0) {
        history.go(goIndex);
      }
    } else {
      history.push(route);
    }
  }
  public back() {
    history.goBack();
  }
  public replace(route: string) {
    history.replace(route);
  }
  public getLength() {
    return this.__getHisArry().findIndex(h => h.pathname === history.location.pathname) + 1;
  }
  public getCurrentRoute() {
    return history.location.pathname;
  }
  public getHistory() {
    return history;
  }
  private __getHisArry() {
    return ((history as any)["entries"] || []) as { pathname: string }[];
  }
}
export const initializing = async () => {
  history = isWeb() ? createBrowserHistory() : createMemoryHistory();
};
